lambda_seq <- seq(1,.305,-.005)^15
i = 0
lasso.results <- list()
  i = i + 1
  # Fit LASSO
  lasso.results[[i]] <- list()
  N <- length(dta.train[,dv])
  cv_results <- foreach (cv=1:cv.runs, 
                         .combine='rbind',
                         .packages = c("glmnet","pROC")) %dopar% {   # Loop over CV runs
     set.seed(52*cv) 
     shuffle <- sample(1:N,
                       N,
                       replace=FALSE)
     pred.prob <- matrix(NA,nrow=N,ncol=length(lambda_seq))
     sampleSplit = c(0,round((N/5*c(1:5))))
     for (f in 1:5) {
       # Fit lasso
       cv.fit <- glmnet(x = as.matrix(dta.train[-shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                                                    rhs]),
                        y = as.matrix(dta.train[-shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                                                    dv]),
                        alpha = lasso_params$alpha,
                        family = lasso_params$family,
                        lambda = lambda_seq)
       pred.prob[shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                 1:length(cv.fit$lambda)] <- predict(cv.fit,
                                                     newx = as.matrix(dta.train[shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                                                                                       rhs]),
                                                                              type = "response")
    }
     cv_results <- c("CV Run"=0,
                     "lambda"=0,
                     "AUC"=0)
     # Get error rates for each lambda used
     for (l in seq(10, 10*(round(length(lambda_seq)/10)-1),
                   10)) {
       if (sum(is.na(pred.prob[,l]))==0) {
         auc <- roc(dta.train[,dv],pred.prob[,l])$auc
         if (auc>cv_results["AUC"]) {
           cv_results <- c("CV Run"=cv,
                           "lambda"=lambda_seq[l],
                           "AUC" = auc)
           best.l <- l
         }
       }
     }
     for (l in seq(best.l-9,best.l+10,1)) {
       if (sum(is.na(pred.prob[,l]))==0) {
         auc <- roc(dta.train[,dv],pred.prob[,l])$auc
         if (auc>cv_results["AUC"]) {
           cv_results <- c("CV Run"=cv,
                           "lambda"=lambda_seq[l],
                           "AUC" = auc)
           best.l <- l
         }
       }
     }
     cv_results<- c(cv_results,
                    "DT" = as.numeric(mostaccDT(y=dta.train[,dv],
                                                yhat=pred.prob[,best.l],
                                                J=1000)))
     return(cv_results)
  } 
  hours <- floor((proc.time()[3]-start.time)/3600)
  mins <- floor((proc.time()[3]-start.time)/60) - hours*60
  secs <- floor((proc.time()[3]-start.time)) - hours*3600 - mins*60
  
  print(paste(hours,
              "h",
              mins,
              "m",
              secs,
              "s elapsed",
              sep = ""))
  
  optim.lambda <- mean(cv_results[,"lambda"]) 
  lasso.results[[i]]$mod <- glmnet(x = as.matrix(dta.train[,
                                                          rhs]),
                                  y = as.matrix(dta.train[,
                                                         dv]),
                                  lambda = seq(10.9,1,-.1)^4*optim.lambda,
                                  alpha = lasso_params$alpha,
                                  family = lasso_params$family
  )
  opt.lam <- 100
  while (is.na(lasso.results[[i]]$mod$lambda[opt.lam])) {
    opt.lam = opt.lam - 1
  }
  lasso.results[[i]]$lambda <- optim.lambda
  lasso.results[[i]]$dt <- mean(cv_results[,"DT"]) 
  lasso.results[[i]]$fit.oos <- predict(lasso.results[[i]]$mod,
                                       newx = as.matrix(dta.test[,
                                                                 rhs]),
                                       type = "response")[,opt.lam]
  lasso.results[[i]]$actual.oos <- as.matrix(dta.test[,
                                                      dv])
  

  save(lasso.results,file=paste(modeldir,"/",
                                country,
                                "_lasso_",
                                v,
                                "_",
                                rhs.group,
                                "_cross",
                                fileext,
                                ".RData",
                                sep = ""))